package com.termine.jsf;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.termine.bean.Bean;
import com.termine.entity.Location;
import com.termine.entity.PersonalData;
import com.termine.util.Logger;

@ManagedBean
@ViewScoped
public class PersonalDataJSF extends Bean implements Serializable {

	private static final long serialVersionUID = -1352747632642616139L;

	private PersonalData personalData;
	private Location location;
	private String confirmPassword;
	private String oldPassword;
	private String newPassword;

	/*
	 * Init
	 */
	@PostConstruct
	public void init() {
		try {
			personalData = (PersonalData) getLoginJSF().getPersonalData()
					.clone();
			location = (Location) getLoginJSF().getLocation().clone();
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
		}
	}

	/*
	 * Jsf Action Methods
	 */
	public String updatePersonalData() {
		try {
			getLoginJSF().setPersonalData((PersonalData) getPersonalData().clone());
			getLoginJSF().setLocation((Location) getLocation().clone());
			getPersonalDataDAO().updatePersonalData(getLoginJSF().getPersonalData());
			getLocationDAO().updateLocationForUser(getLocation(), getPersonalData());
			return "SUCCESS";
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
			return "ERROR";
		}
	}

	public String updateLocation() {
//		try {
//			getLoginJSF().setPersonalData((PersonalData) getPersonalData().clone());
//			getLoginJSF().setLocation((Location) getLocation().clone());
//			getPersonalDataDAO().updatePersonalData(getLoginJSF().getPersonalData());
//			getLocationDAO().updateLocationForUser(getLocation(), getPersonalData());
//			return "SUCCESS";
//		} catch (Exception e) {
//			Logger.log(e.getMessage());
//			e.printStackTrace();
//			return "ERROR";
//		}
		return "SUCCESS";
	}

	public String updatePassword() {
		try {
			if (newPassword.equals(confirmPassword)
					&& oldPassword.equals(getLoginJSF().getPersonalData()
							.getPassword())) {
				getLoginJSF().getPersonalData().setPassword(newPassword);
				getPersonalData().setPassword(newPassword);
				getPersonalDataDAO().updatePersonalData(getPersonalData());
				return "SUCCESS";
			} else {
				return "ERROR";
			}
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
			return "ERROR";
		}
	}

	/*
	 * Getters & Setters
	 */
	public PersonalData getPersonalData() {
		return personalData;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public void setPersonalData(PersonalData personalData) {
		this.personalData = personalData;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}
}
